import datetime
from other.cfg import Config
from other.change_format import change_format
from other.sql_montage import sqlMontage
from other.status import Status
from server.Mysql import MySQL


class feed_back:
    def __init__(self):
        self.cfg = Config()
        self.st = Status()
        self.db = MySQL(self.cfg.res['mysql']['database'], 'feedback')
        self.cf = change_format()
        self.sm = sqlMontage()

    def get_feed_back_list(self, data):
        result = {'msg': '成功', 'code': 200, 'data': [], 'total': None}
        sqls = "where 1=1 "
        sql = ""
        pages = ''
        if data != {}:
            if data.get('pageNum') is not None and data.get('pageSize') is not None:
                if data['pageNum'] != '' and data['pageSize'] != '':
                    pages = " limit {},{}".format(int(data['pageNum']) * int(data['pageSize']) - int(data['pageSize']),
                                                  int(data['pageSize']))
            if data.get('status') is not None:
                if data['status'] != '':
                    sql = sql + " and `status`='{}'".format(data['status'])
            if data.get('beginTime') is not None and data.get('endTime') is not None:
                if data['beginTime'] != '' and data['endTime'] != '':
                    sql = (sql + " and ('{}'<=`create_time` and `create_time`<='{}')".
                           format(data['beginTime'], data['endTime']))
        total = self.db.get(sqls + sql)
        result['total'] = len(total)
        if sql != "":
            sql = sqls + sql + ' ORDER BY `order_num` ASC ' + f'{pages}'
        else:
            sql = sqls + ' ORDER BY `order_num` ASC ' + f'{pages}'
        res = self.db.get(sql)
        if res != ():
            for r in res:
                for key, value in r.items():
                    if isinstance(value, datetime.datetime):
                        r[key] = value.strftime("%Y-%m-%d %H:%M:%S")
                result['data'].append(r)
        result = self.cf.changes_format(result)
        return result

    # 新增反馈
    def feed_back_add(self, data, userInfo):
        result = {'msg': '成功', 'code': 200}
        times = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        data['create_by'] = userInfo['userName']
        data['create_time'] = times
        add_sql = self.sm.add_sql_data(data, 'feedback')
        # print('add_sql', add_sql)
        self.db.adds(add_sql, 'feedback')
        return result

    # 修改反馈
    def feed_back_edit(self, data, userInfo):
        result = {'msg': '成功', 'code': 200}
        times = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        if 'createTime' in data:
            del data['createTime']
        data['update_by'] = userInfo['userName']
        data['update_time'] = times
        edit_sql = self.sm.edit_sql_data(data, 'feedback', 'feedback_id', data['feedbackId'])
        self.db.edits(edit_sql, 'feedback')
        return result

    # 删除反馈
    def feed_back_delete(self, data, userInfo):
        result = {'msg': '成功', 'code': 200}
        times = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        ids = [int(x) for x in data.split(',')]
        for i in ids:
            sql = "where feedback_id='{}'".format(i)
            self.db.delete(sql)
        return result
